package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.db.User;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.Visitors;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Long queryEachLoveCount(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, Friend.class);
    }

    @Override // 我的粉丝 就是喜欢我的人
    public Long queryFanCount(Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override //我喜欢的人
    public Long queryLoveCount(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);

    }

    @Override //查询好友表
    public PageResult queryEachLoveList(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(PageRequest.of(page-1,pagesize, Sort.by(Sort.Direction.DESC,"created")));
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);
        return new PageResult(page,pagesize,(int) count,friendList);
    }

    @Override  //我的喜欢
    public PageResult queryLoveList(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(PageRequest.of(page-1,pagesize, Sort.by(Sort.Direction.DESC,"created")));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageResult(page,pagesize,(int) count,userLikeList);
    }

    @Override   //我的粉丝
    public PageResult queryFansList(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        query.with(PageRequest.of(page-1,pagesize, Sort.by(Sort.Direction.DESC,"created")));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageResult(page,pagesize,(int) count,userLikeList);
    }

    @Override  //我的访问记录
    public PageResult queryVisitorsList(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(PageRequest.of(page-1,pagesize, Sort.by(Sort.Direction.DESC,"date")));
        List<Visitors> visitorsList = mongoTemplate.find(query, Visitors.class);
        long count = mongoTemplate.count(query, Visitors.class);
        return new PageResult(page,pagesize,(int) count,visitorsList);
    }

    @Override
    public void delete(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        mongoTemplate.remove(query,UserLike.class);
    }

    @Override
    public void save(Long userId, Long likeUserId) {
        UserLike userLike = new UserLike();
        userLike.setId(new ObjectId());
        userLike.setCreated(new Date().getTime());
        userLike.setUserId(userId);
        userLike.setLikeUserId(likeUserId);
        mongoTemplate.save(userLike);
    }

    @Override
    public Boolean queryMyFans(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        return mongoTemplate.exists(query, UserLike.class);
        //return mongoTemplate.find(query, UserLike.class);
    }




}
